export default {
    data() {
        return {
            
        }
    },
    computed: {
        basin() {
            return this.$store.getters.basin
        },
        basinArry() {
            return this.$store.getters.basinArry
        }
    },
    watch: {
        basin: {
            handler: function (name) {
                this.$nextTick(() => {
                    this.creatB3DM(name)
                })
            },
            immediate: true
        }
    },
    beforeDestroy() {
        this.clearB3DM()
    },
    created() {
        this.cesium3DTilesetsMap = new Map()
        this.$eventBus.$on('flyToB3DM', this.flyToB3DM)
    },
    methods: {
        flyToB3DM(name) {
            var primitive = this.cesium3DTilesetsMap.get('Tile/' + name)
            if (primitive) {
                this.$viewer.flyTo(primitive, {
                    duration: 0.5,
                    offset: {
                        heading: this.$viewer.camera.heading,
                        pitch: this.$viewer.camera.pitch,
                        range: 5000
                    }
                })
            }
        },
        creatB3DM() {
            this.clearB3DM()
            if (this.layerList2[0].children.B3DM.show) {
                var b3dmBasin = this.basinArry[this.basin].b3dmBasin;
                b3dmBasin.forEach(name => {
                    var url = `${mapConfig.tilesUrlPre}/model/${name}/tileset.json`
                    var cesium3DTileset = new Cesium.Cesium3DTileset({
                        // debugColorizeTiles: true, // 为每个图块分配随机颜色。
                        url: url,
                        maximumScreenSpaceError: 32,
                        cullRequestsWhileMovingMultiplier: 10, // 值越小能够更快的剔除
                        maximumMemoryUsage: 256, // 内存分配变小有利于倾斜摄影数据回收，提升性能体验
                        // cullWithChildrenBounds: false,
                        // dynamicScreenSpaceErrorHeightFalloff: 0.9,
                        // skipLevels: 0
                    })
                    var _this = this;
                    cesium3DTileset.readyPromise.then(tileset => {
                        console.log("加载完成")
                        // 获取3Dtlies的bounds范围
                        const boundingSphere = tileset.boundingSphere;
                        // 获取3Dtlies的范围中心点的弧度
                        const cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center);
                        setTimeout(() => {
                            const heightOffset = 0; // 要改变的高度
                            // 倾斜摄影本身的位置
                            const surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height);
                            // 倾斜摄影高度改变的位置
                            const offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height + heightOffset);

                            // 计算模型的平移变换矩阵
                            let translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());

                            // 设置模型的模型矩阵，将其偏移至指定位置
                            tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);

                            // 将模型添加到场景的图元集合中
                            var primitive = _this.$viewer.scene.primitives.add(tileset)
                            _this.cesium3DTilesetsMap.set(name, primitive)
                        }, 1500)

                    }).catch(function (error) {
                        console.log(error);
                    });
                    // var primitive = this.$viewer.scene.primitives.add(cesium3DTileset)
                    // this.cesium3DTilesetsMap.set(name, primitive)
                })
            } else {
                this.clearB3DM()
            }
        },
        clearB3DM() {
            if (this.cesium3DTilesetsMap.size > 0) {
                this.cesium3DTilesetsMap.forEach((val, key) => {
                    this.$viewer.scene.primitives.remove(val)
                })
                this.cesium3DTilesetsMap = new Map()
            }
        }
    }
}
